app.controller("taskInfoController", function ($scope, $controller, $location, taskInfoService, $http) {

    $controller("baseController", {$scope: $scope});

    //定义条件查询对象
    $scope.searchEntity = {"taskBatchNo": null};

    //获取打款记录列表
    $scope.findPage = function (pageNum, pageSize) {

        if (pageNum == null) pageNum = 1;

        if (pageSize == null) pageSize = 10;

        if (startTime != null) {
            $scope.searchEntity.startTime = new Date(Date.parse(startTime.replace(/-/g, "/")));
        }

        if (endTime != null) {
            $scope.searchEntity.endTime = new Date(Date.parse(endTime.replace(/-/g, "/")));
        }

        taskInfoService.findPage(pageNum, pageSize, $scope.searchEntity).success(function (response) {

            if (response.code == 200) {
                $scope.taskInfoList = response.data;

                //更新总记录数
                $scope.paginationConf.totalItems = response.total;

            } else {
                alert(response.msg);
            }
        });

    };

    //重置条件查询参数
    $scope.resetSearchEntity = function () {

        $scope.searchEntity = {"taskBatchNo": null};

        $scope.reloadList();
    };

    //根据批次号查询订单明细
    $scope.findOneByBatchNumber = function () {

        //获取地址栏批次号
        var taskBatchNo = $location.search()['taskBatchNo'];

        if (taskBatchNo == null) return;

        //批次号
        $scope.searchEntity.taskBatchNo = taskBatchNo;


    };

    //监控订单状态变量
    $scope.$watch("searchEntity.taskStatus", function (newValue, oldValue) {

        if (newValue == null) return;

        $scope.reloadList();
    });

    //数字四舍五入 num-数字 digit-保留位数
    $scope.numParse = function (num, digit) {

        var s = num.toFixed(digit);

        return s;
    };

    //字符串格式化
    $scope.strParse = function (str) {

        if (str != null && str.indexOf("：") != -1) {
            str = str.substring(str.indexOf("：") + 1, str.length);
        }

        if (str != null && str.indexOf("(") != -1) {
            str = str.split("(")[0];
        }

        if (str != null && str.indexOf("[") != -1) {
            str = str.split("[")[0];
        }

        return str;
    };

    //导出功能
    $scope.download = function () {

        if (!confirm("您确定导出吗？")) return;

        if (startTime != null) {
            $scope.searchEntity.startTime = startTime;
        }

        if (endTime != null) {
            $scope.searchEntity.endTime = endTime;
        }

        recordPaymentService.download($scope.searchEntity).success(function (response) {

            if (response.code == 200) {

                // 文件下载
                window.open(response.msg);

                alert("下载完成");
            } else {
                alert(response.msg);
            }
        })
    };

    $scope.uploadParam = { taskInfoId: null }
    $scope.addTaskInfoId = function (taskInfo) {
        $scope.uploadParam.taskInfoId = taskInfo
    }

    $scope.addTaskInfoProvePic = function () {
        let forData = new FormData();
        const uploadsFile = document.getElementById(`file_task_prove_pic`);
        forData.append("file", uploadsFile.files[0]);
        forData.append("taskInfoId", $scope.uploadParam.taskInfoId);
        $http({
            method: 'POST',
            url: '/tanhaogongManager/taskInfo/uploadProvePic',
            data: forData,
            headers: {'content-Type': undefined},
            transformRequest: angular.identity
        }).success(function (response) {
            console.log(response)
            if (response.code === 200) {
                uploadsFile.outerHTML = uploadsFile.outerHTML
                alert("上传成功")
                $scope.findPage(1,10);
            } else {
                alert(response.msg)
            }
        })
    };

});